package com.yuan.yblog.portal.pojo.VO;

import com.yuan.yblog.crud.entity.User;
import com.yuan.yblog.portal.pojo.contants.Status;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collection;
import java.util.Date;

/**
 * @author hepeiyuan on 2024/5/14 10:30
 * @description
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "用户传输对象")
public class UserVO extends User {

    Collection<SimpleGrantedAuthority> authorities;

    /**
     * UserVO转为User
     */
    public User toUser() {
        User user = new User();
        user.setId(this.getId());
        user.setUsername(this.getUsername());
        user.setPassword(null);
        user.setEmail(this.getEmail());
        user.setMobile(this.getMobile());
        user.setPoint(this.getPoint());
        user.setSign(this.getSign());
        user.setGender(this.getGender());
        user.setWechat(this.getWechat());
        user.setVipLevel(this.getVipLevel());
        user.setBirthday(this.getBirthday());
        user.setAvatar(this.getAvatar());
        user.setPostCount(this.getPostCount());
        user.setCommentCount(this.getCommentCount());
        user.setCollectCount(this.getCollectCount());
        user.setStatus(this.getStatus());
        user.setLasted(this.getLasted());
        user.setCreated(this.getCreated());
        user.setModified(this.getModified());
        return user;
    }

    /**
     * User转UserVO
     */
    public static UserVO getUseVO(User user, Collection<SimpleGrantedAuthority> authorities) {
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        // 配置可获取的信息
        userVO.setAuthorities(authorities);
        userVO.setId(user.getId());
        userVO.setUsername(user.getUsername());
        userVO.setPassword(null);
        userVO.setEmail(user.getEmail());
        userVO.setMobile(user.getMobile());
        userVO.setPoint(user.getPoint());
        userVO.setSign(user.getSign());
        userVO.setGender(user.getGender());
        userVO.setWechat(user.getWechat());
        userVO.setVipLevel(user.getVipLevel());
        userVO.setBirthday(user.getBirthday());
        userVO.setAvatar(user.getAvatar());
        userVO.setPostCount(user.getPostCount());
        userVO.setCommentCount(user.getCommentCount());
        userVO.setCollectCount(user.getCollectCount());
        userVO.setStatus(user.getStatus());
        userVO.setLasted(user.getLasted());
        userVO.setCreated(user.getCreated());
        userVO.setModified(user.getModified());
        return userVO;
    }


    /**
     * 用户新增实体
     */
    public User toUserInsertEntity () {
        User user = this.toUser();
        user.setId(null);
        user.setPoint(0);
        user.setVipLevel(0);
        user.setPostCount(0);
        user.setCommentCount(0);
        user.setCollectCount(0);
        user.setStatus(Status.valid);
        user.setLasted(new Date());
        user.setCreated(new Date());
        user.setModified(new Date());
        return user;
    }

    /**
     * 用户资料更新实体
     */
    public User toUserInfoUpdateEntity() {
        User user = new User();
        user.setId(this.getId());
        user.setUsername(this.getUsername());
        user.setEmail(this.getEmail());
        user.setSign(this.getSign());
        user.setWechat(this.getWechat());
        user.setBirthday(this.getBirthday());
        user.setModified(this.getModified());
        return user;
    }

    /**
     * 转化为基础用户资料（他人可获取），抹去敏感信息
     */
    public static UserVO getBasicInfoVO(User user, Collection<SimpleGrantedAuthority> authorities) {
        if (user == null) {
            return null;
        }
        UserVO userVO = getDetailedInfoVO(user, authorities);
        // 进一步抹去更多信息
        userVO.setEmail(null);
        userVO.setMobile(null);
        userVO.setStatus(null);
        userVO.setWechat(null);
        return userVO;
    }

    /**
     * 转化为用户拥有者资料（仅自己能获取），抹去敏感信息
     */
    public static UserVO getDetailedInfoVO(User user, Collection<SimpleGrantedAuthority> authorities) {
        if (user == null) {
            return null;
        }
        UserVO userVO = UserVO.getUseVO(user, authorities);
        // 配置可获取的信息
        userVO.setPassword(null);
        userVO.setModified(null);
        return userVO;
    }
}
